#include "gui/objects/GUIObjectsContainer.h"

namespace gui {

GUIObjectsContainer::GUIObjectsContainer() {
	this->leftmost = 0;
	this->rightmost = 0;
	this->topmost = 1;
	this->bottommost = 1;
}

GUIObjectsContainer::~GUIObjectsContainer() {
	while (!(this->guiObjects.isEmpty())) {
		delete (this->guiObjects.removeFirst());
	}
}

util::LinkedList<GUIObject> GUIObjectsContainer::getGUIObjects() {
	return this->guiObjects;
}


void GUIObjectsContainer::addObjectToContainer(GUIObject* object) {
	bool firstTime = this->guiObjects.isEmpty();
	this->guiObjects.append(object);
	this->updateLimits(object, firstTime);
}

void GUIObjectsContainer::updateLimits(GUIObject* object, bool firstTime) {
	int leftmost = object->getXPos() - object->getWidth() / 2;
	int rightmost = object->getXPos() + object->getWidth() / 2;
	int topmost = object->getYPos() - object->getHeight() / 2;
	int bottommost = object->getYPos() + object->getHeight() / 2;
	if (firstTime) {
		this->leftmost = leftmost;
		this->rightmost = rightmost;
		this->topmost = topmost;
		this->bottommost = bottommost;
	} else {
		this->leftmost = min(leftmost, this->leftmost);
		this->rightmost = max(rightmost, this->rightmost);
		this->topmost = min(topmost, this->topmost);
		this->bottommost = max(bottommost, this->bottommost);
	}
}


}
